import 'package:flutter/material.dart';
import 'package:untitled1/app/broker/modules/my/PersonalInfoPage.dart';
import 'package:untitled1/app/pay/PayPage.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/utils/utilDialog.dart';
import 'package:untitled1/app/utils/utilRouter.dart';
import 'package:untitled1/app/widgets/toast/MyToast.dart';
import '../../../pay/showLockInfoDialog.dart';
import '../../../utils/utilScreen.dart';
import '../../../widgets/base_page/BaseStatefulPage.dart';
import '../../../bean/KeyValueBean.dart';
import '../employer/employer_page/employer_page_dialog/EmployerPageDialog.dart';
import 'BrokeragePage.dart';
import 'ContractManagePage.dart';
import 'RemainingMoneyPage.dart';
import 'SettingsPage.dart';
import '../employer/EmployerDetailUpdate.dart';

//我的
class MyPage extends BaseStatefulPage {
  const MyPage({super.key}) : super();

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() => _MyPageState();
}

class _MyPageState extends BaseStatefulPageState<MyPage> {
  String? _error;
  bool _isEmpty = false;

  @override
  void initState() {
    super.initState();
    // 页面初始化，加载数据
    loadData();
  }

  void loadData() async {
    safeSetState(() {
      _error = null;
      _isEmpty = false;
    });

    await Future.delayed(const Duration(seconds: 2));

    // 模拟异常或者空数据
    safeSetState(() {
      _error = null; // "网络异常";
      _isEmpty = false;
    });
  }

  @override
  Widget buildPage(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffFFF2F2),
      body: Container(
        width: utilScreen.getScreenWidth(context),
        height: utilScreen.getScreenHeight(context),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: const AssetImage('assets/app/gerenzhongxin_bg.png'),
            fit: BoxFit.cover
          )
        ),
        child: Column(
          children: [
            SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton(
                  onPressed: () {
                    utilRouter.to(SettingsPage());
                  },
                  child: Image.asset(
                    'assets/app/shezhi.png',
                    width: 30,
                    height: 30,
                  ),
                ),
              ],
            ),
            Container(
              padding: const EdgeInsets.all(16),
              child: GestureDetector(
                onTap: () {
                  utilRouter.to(PersonalInfoPage());
                },
                child: Row(
                  children: [
                    const CircleAvatar(
                      radius: 35,
                      child: Icon(
                        Icons.person_rounded,
                        size: 30,
                        color: Colors.white,
                      ),
                      // backgroundImage: NetworkImage(
                      //     'https://avatars.githubusercontent.com/u/583231?v=4'),
                    ),
                    const SizedBox(width: 16),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              const Text('名称', style: TextStyle()),
                              const SizedBox(width: 10),
                              // Text('师傅'),
                              //shifu.png,tudi.png,weijueze.png
                              Image.asset('assets/app/tudi.png',width: 30,height: 15),
                              // Image.asset('assets/app/shifu.png',width: 30,height: 13),
                              // Image.asset('assets/app/shifu.png',width: 30,height: 15),
                            ],
                          ),
                          const SizedBox(height: 8),
                          const Text('昵称', style: TextStyle(color: Colors.grey)),
                        ],
                      ),
                    ),
                    const SizedBox(width: 10),
                    GestureDetector(
                      child: Container(
                        child: Row(
                          children: [
                            Text(
                              '编辑个人资料',
                              style: TextStyle(color: Colors.grey),
                            ),
                            const Icon(Icons.arrow_forward_ios, size: 16),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 10),
            Container(
              height: 80,
              padding: const EdgeInsets.only(left: 15, right: 15),
              child: Row(
                children: [
                  Expanded(child: getViewDjs1(0, "合同管理",onTap: () {
                    utilRouter.to(ContractManagePage());
                  })),
                  SizedBox(width: 10),
                  Expanded(child: getViewDjs1(0, "待结算(元)",onTap:(){
                    utilRouter.to(BrokeragePage());
                  })),
                  SizedBox(width: 10),
                  Expanded(child: getViewDjs1(0, "账户余额(元)",onTap: (){
                    utilRouter.to(RemainingMoneyPage());
                  })),
                ],
              ),
            ),
            const SizedBox(height: 10),
            Container(
              width: MediaQuery.of(context).size.width,
              margin: const EdgeInsets.only(left: 10, right: 10),
              decoration: BoxDecoration(borderRadius: BorderRadius.circular(3)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 10),
                  const Text(
                    '我的合同',
                    style: TextStyle(
                      color: myColors.c_191919,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 8),
                  Row(
                    children: [
                      // ("assets/app/logo.png",34343434,"待计算")
                      Expanded(
                        child: getViewWdht('assets/app/quanbuhetong.png', '全部合同', 0),
                      ),
                      Expanded(
                        child: getViewWdht('assets/app/daiqianshu.png', '待签署', 0),
                      ),
                      Expanded(
                        child: getViewWdht('assets/app/daiwoqian.png', '带我签署', 0),
                      ),
                    ],
                  ),
                  SizedBox(height: 8),
                  Row(
                    children: [
                      Expanded(
                        child: getViewWdht('assets/app/daiqianshuwancheng.png', '签署完成', 0),
                      ),
                      Expanded(
                        child: getViewWdht('assets/app/yizuofei.png', '已作废', 0),
                      ),
                      Expanded(child: Container()),
                      // getViewWdht('','',0),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget getViewWdht(String img, String mc, int sl) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          EmployerPageDialog.servicingTime(context, (KeyValueBean bean){

          });
        },
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Image.asset(img, height: 40, width: 40),
              SizedBox(height: 10),
              Text('${mc}(${sl})', style: TextStyle(fontSize: 12)),
            ],
          ),
        ),
      ),
    );
  }

  Widget getViewDjs1(int je, String bt,{VoidCallback? onTap,}) {
    return InkWell(
      onTap: () {
        onTap?.call();
      },
      child: Container(
        height: 80,
        width: 120,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5)),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  "${je}",
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: myColors.c_191919,
                  ),
                ),
                Text("${bt}"),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget getViewDjs(String imgstr, int je, String bt) {
    return Container(
      height: 80,
      width: 120,

      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(5)),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset("${imgstr}", width: 20, height: 20),
          SizedBox(width: 10),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "${je}",
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: myColors.c_191919,
                ),
              ),
              Text("${bt}"),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildItem(IconData icon, String title, {int? type}) {
    return Container(
      color: Colors.white,
      child: ListTile(
        leading: Icon(icon, color: Colors.black),
        title: Text(title),
        trailing: const Icon(Icons.arrow_forward_ios, size: 16),
        onTap: () {
          print(">]title:${title}");
          if (title == "设置") {
            utilRouter.to(SettingsPage());
          } else if (title == '完善雇主信息') {
            utilRouter.to(EmployerDetailUpdate());
          } else if ('支付' == title) {
            utilRouter.to(PayPage());
          } else if ('支付弹出框' == title) {
            utilDialog.payDialog(context);
            // showLockInfoDialog(context);
          } else if ('toast' == title) {
            MyToast.showSystemToast("这是一个系统Toast");
          }
        },
      ),
    );
  }
}
